package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabyFootBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BabyFootPresenter;
import com.android.infantschool.model.mvp.presenter.BabyFootPresenterImpl;
import com.android.infantschool.model.mvp.view.BabyFootView;
import com.android.infantschool.ui.adapter.BabyFootAdapter;
import com.android.infantschool.ui.view.calendar.bean.DateEntity;
import com.android.infantschool.ui.view.calendar.view.DataView;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\20 0020 15:32
 * @class describe
 */
public class BabyFootActivity extends BaseActivity implements BabyFootAdapter.OnLister, BabyFootView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.week)
    public DataView week;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    private BabyFootAdapter adapter;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, BabyFootActivity.class);
        return intent;
    }

    private BabyFootPresenter footPresenter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_foot);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        footPresenter = new BabyFootPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("宝宝饮食");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BabyFootAdapter(this, this);
        recyclerView.setAdapter(adapter);

        BabyFootActivity.this.showLoading();
        week.setOnSelectListener(new DataView.OnSelectListener() {
            @Override
            public void onSelected(DateEntity date) {
                adapter.clear();
                BabyFootActivity.this.showLoading();
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("time", date.date);
                Log.e(TAG, "map:" + map.toString());
                footPresenter.getBabyFoot(map);
                BabyFootActivity.this.showLoading();
            }
        });
        week.getData(DateUtil.getDate(DateUtil.ymd));
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (footPresenter != null) {
            footPresenter.onDestroy();
            footPresenter = null;
        }
    }

    @Override
    public void lister(int position) {

    }

    @Override
    public void errorBabyFoot(String error) {
        BabyFootActivity.this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successBabyFoot(BaseBean baseBean) {
        BabyFootActivity.this.dismissLoading();
        BabyFootBean footBean = (BabyFootBean) baseBean;
        if (footBean.getCode().equals("0")) {
            adapter.setData(footBean.getData());
        } else {
            ToastUtil.showShortToast(this,"获取失败:" + footBean.getMsg());
        }
    }
}